package com.zl.roomdesign.main

import android.Manifest
import android.graphics.Color
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.view.View
import com.zl.core.base.BaseFragment
import com.zl.core.base.ModeActivity
import com.zl.roomdesign.R
import com.zl.roomdesign.main.dynamic.DynamicFragment
import com.zl.roomdesign.main.mainpage.MainPageFragment
import com.zl.roomdesign.main.mime.MineFragment
import kotlinx.android.synthetic.main.activity_main.*

/**
 * Created by zhangli on 2018/5/6,09:22<br/>
 */
class MainActivity : ModeActivity() {

    var selectItem = 0
    private var currentFragment: BaseFragment? = null
    private var mainPageFragment: BaseFragment? = null
    private var dynamicFragment: BaseFragment? = null
    private var mineFragment: BaseFragment? = null

    override fun layoutId() = R.layout.activity_main

    override fun initView(savedInstanceState: Bundle?) {
    }

    override fun setListener() {

        mainPageText.setOnClickListener {
            clickMain()
        }

        dynamicText.setOnClickListener {
            clickDynamic()
        }

        mineText.setOnClickListener {
            clickMine()
        }
    }

    override fun observe() {
    }

    override fun afterView() {
        clickMain()

        requestPermission(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE), {
            if (!it) {
                finish()
            }
        })
    }

    private fun changeSelectItem(i: Int): Boolean {
        if (i == selectItem) {
            return true
        } else {
            val colorUnselected = ContextCompat.getColor(this, com.zl.core.R.color.txt_unselected)
            when (selectItem) {
                1 -> {
                    mainPageText.textSize = 18f
                    mainPageText.setTextColor(colorUnselected)
                    mainPageImg.visibility = View.GONE
                }
                2 -> {
                    dynamicText.textSize = 18f
                    dynamicText.setTextColor(colorUnselected)
                    dynamicImg.visibility = View.GONE
                }
                3 -> {
                    mineText.textSize = 18f
                    mineText.setTextColor(colorUnselected)
                    mineImg.visibility = View.GONE
                }
            }
            when (i) {
                1 -> {
                    mainPageText.textSize = 20f
                    mainPageText.setTextColor(Color.WHITE)
                    mainPageImg.visibility = View.VISIBLE
                }
                2 -> {
                    dynamicText.textSize = 20f
                    dynamicText.setTextColor(Color.WHITE)
                    dynamicImg.visibility = View.VISIBLE
                }
                3 -> {
                    mineText.textSize = 20f
                    mineText.setTextColor(Color.WHITE)
                    mineImg.visibility = View.VISIBLE
                }
            }
            selectItem = i
//            (activity as OnFragmentChangeListener).onFragmentChange(selectItem)
            return false
        }

    }

    private fun clickMain() {
        if (!changeSelectItem(1)) {
            if (mainPageFragment == null) {
                mainPageFragment = MainPageFragment()
            }
            showFragment(mainPageFragment!!)
        }
    }

    private fun clickDynamic() {

        if (!changeSelectItem(2)) {
            if (dynamicFragment == null) {
                dynamicFragment = DynamicFragment()
            }
            showFragment(dynamicFragment!!)
        }

    }

    private fun clickMine() {

        if (!changeSelectItem(3)) {
            if (mineFragment == null) {
                mineFragment = MineFragment()
            }
            showFragment(mineFragment!!)
        }

    }

    private fun showFragment(fragment: BaseFragment) {
        if (fragment != currentFragment) {
            val transaction = supportFragmentManager.beginTransaction()

            val fragmentByTag = supportFragmentManager.findFragmentByTag(fragment::class.java.simpleName)
            if (fragmentByTag != null) {
                transaction.show(fragmentByTag)
            } else {
                transaction.add(R.id.frameLayout, fragment, fragment::class.java.simpleName)
            }
            if (currentFragment != null) {
                transaction.hide(currentFragment)
            }

            transaction.commit()
            currentFragment = fragment
        }
    }
}